import { Body, Controller,  Post, Request } from "@nestjs/common";
import { GateAuthService } from "./gateAuth.service";
import { JwtService } from "@nestjs/jwt";
import { NoNeedLogin } from "../../../../libs/decorator/no-need-login/no-need-login.decorator";
import { Constants } from "../../../../types/types";
import { LogService } from "@app/log";

@Controller("oauth")
export class GateAuthController {
  constructor(
    private readonly jwtService: JwtService,
    private readonly logService: LogService,
    private readonly gateAuthService: GateAuthService
  ) {}

  @Post("test")
  @NoNeedLogin()
  test(){
    return "aaaa"
  }

  @NoNeedLogin()
  @Post("token")
  async signIn(@Body() signInDto: Record<string, any>) {
    const user = await this.gateAuthService.signIn(
      signInDto.username,
      signInDto.password,
    );
    this.logService.log(`signIn user` ,user);
    const payload = {
      userId: user.id,
      username: user.username,
    };
    return {
      code: Constants.SuccessCode,
      access_token: await this.jwtService.signAsync(payload),
      msg: "success",
      data: {
        userId: user.id,
      },
    };
  }

  @Post("profile")
  getProfile(@Request() req) {
    return {
      code: Constants.SuccessCode,
      data: {
        user: req.user,
      },
      msg: "success",
    };
  }
}
